package com.wthink.newMain.settings.menuManager.inner.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.wthink.hzgj.data.BusRepository
import com.wthink.hzgj.data.repository.DefaultBusRepository
import com.wthink.hzgj.home.data.Line
import com.wthink.hzgj.util.Page
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class MenuManagerLineViewmodel : ViewModel(){

    private val busRepository: BusRepository = DefaultBusRepository()

    val lineList = MutableLiveData(
        Page(
            emptyList<Line>(), 0, 1
        )
    )

    fun start(){
        viewModelScope.launch(Dispatchers.IO) {
            if (lineList.value!!.list.isEmpty()) {
                lineList.postValue(
                    busRepository.getLinePage(lineList.value!!.page, true)
                )
            }
/*
            busRepository.refresh()?.let {
                it.join()

                lineList.postValue(
                    busRepository.getLinePage(lineList.value!!.page, true).apply {
                        val selectId = getSelectLine()?.id
                        if (selectId != null) {
                            list.find { it.id == selectId }?.select = true
                        }
                    }
                )
            }
*/
        }
    }

    fun getSelectLine(): Line? {
        val line = lineList.value!!.list.find { it.select }
        return line
    }

}